
import { DecimalCalculator as calc } from "./math"

export const getDomData = () => {    
    const result = {
        fba: 0, shoujia: 0, chang: 0, kuan: 0, gao: 0, zhong: 0
    }
    const listing = document.querySelector("#seller-sprite-extension-quick-view-listing")
    if (!listing) {
        alert('没找到数据节点')
        return
    }
    // 获取FBA费用
    const domArr1 = listing.querySelectorAll('.exts-color-border-black.ml-ext-3.el-tooltip__trigger.el-tooltip__trigger')
    if (domArr1[0]) {
        const s = +((domArr1[0] as any).getHTML() as string).replace('$', '')
        if (!isNaN(s)) {
            result.fba = s
        }
    }
    // 获取售价
    const domArr2 = listing.querySelectorAll('.exts-color-border-black.grade-hover.el-tooltip__trigger.el-tooltip__trigger')
    if (domArr2[0]) {
        const s = +((domArr2[0] as any).getHTML() as string).replace('$', '')
        if (!isNaN(s)) {
            result.shoujia = s
        }
    }
    // 获取长宽高
    const domArr3 = listing.querySelectorAll('.exts-color-border-black.el-tooltip__trigger.el-tooltip__trigger')
    if (domArr3[7]) {
        const [chang, kuan, gao] = ((domArr3[7] as any).getHTML() as string).replace(' ', '').split('inches')[0].split('x')
        result.chang = !isNaN(+chang) ? +chang : 0
        result.kuan = !isNaN(+kuan) ? +kuan : 0
        result.gao = !isNaN(+gao) ? +gao : 0
    }
    if (domArr3[4]) {
        const arr = ((domArr3[4] as any).getHTML() as string).split(' ')
        if (arr[1] === 'pounds') {
            result.zhong = +arr[0]
            // 盎司转换成磅
        } else if (arr[1] === 'ounces') {
            result.zhong = calc.divide(+arr[0], 16)
        }
    }
    return result
}